﻿using System;

using System.Windows.Automation;
using System.Diagnostics;
using System.Threading;

namespace TestScenario
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Process p = Process.Start("..\\..\\..\\WpfApplication1\\bin\\Debug\\WpfApplication1.exe");
                Thread.Sleep(2000);

                AutomationElement desktop = AutomationElement.RootElement;
                AutomationElement form = desktop.FindFirst(TreeScope.Children,
                      new PropertyCondition(AutomationElement.NameProperty, "MainWindow"));
                AutomationElement button = form.FindFirst(TreeScope.Children,
                  new PropertyCondition(AutomationElement.NameProperty, "Button"));
                AutomationElement textBox = form.FindFirst(TreeScope.Children,
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
                AutomationElement checkBox = form.FindFirst(TreeScope.Children,
                  new PropertyCondition(AutomationElement.NameProperty, "CheckBox"));


                Automation.AddAutomationEventHandler(
                    InvokePattern.InvokedEvent,
                    button,
                    TreeScope.Element,
                    (object sender, AutomationEventArgs e) => { Console.WriteLine("button InvokePattern.InvokedEvent"); }
                    );

                Automation.AddAutomationEventHandler(
                    TextPattern.TextChangedEvent,
                    textBox,
                    TreeScope.Element,
                    OnTextChanged
                    );

                Automation.AddAutomationPropertyChangedEventHandler(checkBox,
                    TreeScope.Element,
                    (object sender, AutomationPropertyChangedEventArgs e) => { Console.WriteLine("checkBox TogglePattern.ToggleStateProperty " + ((ToggleState)e.NewValue).ToString()); },
                    TogglePattern.ToggleStateProperty
                    );


                Automation.AddAutomationPropertyChangedEventHandler(form,
                    TreeScope.Element,
                    (object sender, AutomationPropertyChangedEventArgs e) => { Console.WriteLine("form WindowPattern.WindowVisualStateProperty " + ((WindowVisualState)e.NewValue).ToString()); },
                    WindowPattern.WindowVisualStateProperty
                    );

                Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent,
                    form,
                    TreeScope.Element,
                    (object sender, AutomationEventArgs e) => { Console.WriteLine("form Exit"); }
                    );

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Fatal error: " + ex.Message);
                Console.ReadLine();
            }

        } // Main()

        static private void OnTextChanged(object src, AutomationEventArgs e)
        {
            AutomationElement eElement = src as AutomationElement;
            String text = "";
            TextPattern targetTextPattern = eElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
            if (targetTextPattern != null)
            {
                text = targetTextPattern.DocumentRange.GetText(-1);
            }
            System.Console.Out.WriteLine("textBox OnTextChanged = \"" + text + "\"");
        }
    }
}
